package com.example.gzysmartcity.ui.fragment.idea;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentIdeaBinding;
import com.example.gzysmartcity.ui.adapter.BrAdapter;
import com.example.gzysmartcity.ui.adapter.IdeaRvAdapter;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.viewmodel.IdeaViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.indicator.CircleIndicator;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class IdeaFragment extends BaseFragment<FragmentIdeaBinding> {
    @Override
    protected FragmentIdeaBinding viewBinding() {
        return FragmentIdeaBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        IdeaViewModel ideaViewModel = new ViewModelProvider(this).get(IdeaViewModel.class);
        ideaViewModel.getHrmData();
        b.rfIdea.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                ToastUtils.showShort("没有更多信息了");
                b.rfIdea.closeHeaderOrFooter();

            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                ToastUtils.showShort("刷新成功");
                ideaViewModel.getHrmData();
            }
        });
        ideaViewModel.getIdeaLiveData().observe(getViewLifecycleOwner(), state -> {
            switch (state) {
                case FAIL:
                    ToastUtils.showShort("请求失败,请尝试下拉重新请求数据。");
                    b.rfIdea.closeHeaderOrFooter();
                    break;
                case SUCCESS:
                    List<String> strLisy = ideaViewModel.getImageIdeaData();
                    IdeaRvAdapter adapter = new IdeaRvAdapter(R.layout.rv_idea, strLisy.size());
                    adapter.setNewInstance(strLisy);
                    b.rvIdea.setAdapter(adapter);
                    b.rvIdea.setLayoutManager(new LinearLayoutManager(getContext()));
                    adapter.setOnItemClickListener((adapter1, view1, position) -> {
                        if (position == strLisy.size() - 1) {
                            ToastUtils.showShort("敬请期待");
                        } else {
                            jump(PositionFragment.class, null, null);
                        }
                    });
                    b.rfIdea.closeHeaderOrFooter();
                    break;
            }
        });
        List<Object> imageBanner = new ArrayList<Object>(Arrays.asList(R.drawable.idea_banner,
                R.drawable.idea_banner1, R.drawable.idea_banner2, R.drawable.idea_banner3, R.drawable.idea_banner5));
        b.bannerIdea.setAdapter(new BrAdapter(imageBanner))
                .addBannerLifecycleObserver(this)
                .setLoopTime(3000)
                .setIndicator(new CircleIndicator(getContext()));
    }

    @Override
    protected String setTitle() {
        return null;
    }

    @Override
    protected boolean showBack() {
        return false;
    }

    @Override
    protected Boolean showBottom() {
        return true;
    }
}